<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\service\CourseService;
use think\Request;

class CourseController extends BaseMarriageOpController
{
    /**
     * @var CourseService
     */
    private $courseService;

    protected function init()
    {
        parent::init();
        $this->courseService = service('Course', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [];
        $res = $this->courseService->pageListByParams($params, $pageSize);
        if(!empty($res['data'])) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $dataList[] = $this->courseService->format($rs);
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }

    public function info(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $course = $this->courseService->getByPk($id);
        $course = $this->courseService->format($course);
        return $this->success(['course' => $course]);
    }

    public function add(Request $request)
    {
        $data = $request->param();
        $this->courseService->create($data);
        return $this->success();
    }

    public function edit(Request $request)
    {
        $data = $request->param();
        $this->courseService->updateByPk($data);
        return $this->success();
    }

    public function sign_in_qrcode(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $img = $this->courseService->getSignInQrCodeImg($id);
        return $this->success(['img' => $img]);
    }
}